// 做头顶目录
let formName = localStorage.getItem('formName')
let categoryId = localStorage.getItem('categoryId')-0
let subCategoryId = localStorage.getItem('subCategoryId')-0

let str =  `<a href="./index.html">首页</a>
            <span>&gt;</span>
            <span>${formName}</span>`
$('.catalog .center').html(str)


// 发送ajax请求 - 获取数据
$.ajax({
    url:'/globalinfo',
    dataType: 'json',
    data: {
        '__timestamp': +new Date()
    },
    success: function (res){
        nav = res.data.cateList
        let str = '<li><a href="javascript:;" class="show">全部</a></li>'
        nav.forEach(item => {
            // 判断categoryId - 获取到这条id下的内容
            if(item.id != categoryId) return false
            item.subCateList.forEach(items=>{
                str += `<li><a href="javascript:;">${items.name}</a></li>`
            })
        });
        $('.list .center .classify ul.left').html(str)
        //点击切换颜色
        $('.classify li a').click(function(){
            $(this).addClass('show')
            $(this).parent().siblings().children().removeClass('show')
        })
        
        // 发送Ajax请求 - 获取商品列表
        $.ajax({
            url:'/item',
            data:{
                __timestamp: + new Date(),
                sortType: 0,
                descSorted: false,
                subCategoryId:subCategoryId,
                deliveryAreaId: 0,
                categoryId: categoryId
            },
            success: function (res){
                let itemList = res.data.itemList
                console.log(itemList)
                let str = ''
                itemList.forEach(item=>{
                    str += `<li><a href="./goods.html">
                        <div class="imgBox">
                            <img src="${item.scenePicUrl}" alt="加载错误">
                            <img src="${item.listPicUrl}" alt="加载错误">
                        </div>
                        <div class="wordBox">
                            <p>${item.name}</p>
                            <p><span>￥${item.retailPrice}</span></p>
                            <p>${item.simpleDesc}</p>
                        </div>
                    </a></li>`
                })
                $('.itemList').html(str)

                // 点击跳转到商品详情页
                $('.itemList li').click(function(){
                    let index = $('.itemList li').index($(this))
                    localStorage.setItem('goodsId',itemList[index].id)
                    localStorage.setItem('goodsName',itemList[index].name)
                })
            },
            error: function(){
                console.log('error')
            }
        })

    },
    error: function(){
        console.log('error')
    }
})

// 点击切换颜色、
$('.area li a').click(function(){
    $(this).addClass('show')
    $(this).parent().siblings().children().removeClass('show')
})
$('.rank li a').click(function(){
    $(this).addClass('show')
    $(this).parent().siblings().children().removeClass('show')
})

